package cn.example.listandrecycleruit;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collections;

public class MainActivity extends AppCompatActivity {

    //数据源
    private String[] data = {"Apple", "Banana", "Orange", "Watermelon",
            "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango",
            "Apple", "Banana", "Orange", "Watermelon",
            "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango"};

    private ListView mListView;
    private ArrayList<Fruit> list=new ArrayList();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mListView = findViewById(R.id.mListView);
        //使用 arraysadpter
        //R.layout.support_simple_spinner_dropdown_item 默认提供的布局类型
        /*ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, data);
        mListView.setAdapter(adapter);*/
        initImgData();
        FruitAdapter adapter = new FruitAdapter(MainActivity.this, R.layout.fruit_item, list);
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Fruit fruit = list.get(i);
                Toast.makeText(MainActivity.this,fruit.getName(),Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initImgData() {
        for (int i = 0; i < 3; i++) {
            Fruit a = new Fruit("a", R.drawable.a);
            Fruit b = new Fruit("b", R.drawable.b);
            Fruit c = new Fruit("c", R.drawable.c);
            Fruit d = new Fruit("d", R.drawable.d);
            Fruit e = new Fruit("e", R.drawable.e);
            list.add(a);
            list.add(b);
            list.add(c);
            list.add(d);
            list.add(e);
        }
    }
}
